// import { taskMixin } from '@/view/research/design/task/taskMixin.js'
import { getToolAccount,startToolTunnel, stopToolTunnel, getToolTunnelDetail} from '@/api/tool-manage.js' 
import { getDictInfoByName } from '@/api/dict.js'
// 要求 prjId和currentSubnetId存在
export default {
    // mixins:[taskMixin],
    data(){
        return {
            toolkitOpenTime:1,
            toolDetail:{},
            toolAcountInfo:{
                userName:'',
                password:'',
                name:'',
            },
            loading:{
                start:false,
                stop:false
            },
            taskStatusObj:{
                initialized:{
                    label:'初始化',
                    value:'initialized',
                    color:"default"
                },
                running:{
                    label:'运行中',
                    value:'running',
                    color:"success"
                },
                failed:{
                    label:'失败',
                    value:'failed',
                    color:"error"
                },
                completed:{
                    label:'完成',
                    value:'completed',
                    color:"primary"
                },
                starting:{
                    label:'启动中',
                    value:'starting',
                    color:"success"
                },
                closing:{
                    label:'关闭中',
                    value:'closing',
                    color:"success"
                },
                error:{
                    label:'失败',
                    value:'failed',
                    color:'error'
                },
            }
        }
	},
    mounted(){
    },
    beforeDestroy(){
        clearInterval(this.toolIntervel);
    },
	methods:{
        getDictInfoByName(){
            return new Promise((resolve,reject) => {
                getDictInfoByName({name:'toolkitOpenTime'}).then(({code,attribute:data,message}) => {
                    if(code === 1){
                        this.toolkitOpenTime = data.data.value || 1;
                        resolve(data.data.value)
                    }else{
                        resolve()
                    }
                }).catch(error => {
                    resolve()
                })
            })
        },
        getToolTunnelDetail(type){
            if(!this.prjId || !this.currentSubnetId){
                return
            }
            return new Promise((resolve,reject) => {
                getToolTunnelDetail(this.prjId,this.currentSubnetId).then(({code,data,message}) => {
                    if(code === 1){
                        this.toolDetail = data;
                        let toolUrl = '';
                        if(data && data.ip){
                            toolUrl = `http://${data.ip}/toolkit/#/login`
                            this.toolDetail.toolUrl = toolUrl;
                        }
                        if((type === 'start' && this.toolDetail.status != 'running') || (type === 'stop' && this.toolDetail.status != 'completed')){
                            this.toolIntervel = setInterval(() => {
                                this.getToolTunnelDetail(type);
                            },3000)
                        }else{
                            clearInterval(this.toolIntervel)
                        }
                        resolve(this.toolDetail)
                    }else{

                    }
                }).catch(error => {
                    console.log(error);
                })
            })
        },
        //获取当前用户工具库账号
        getToolAccount(){
            return new Promise((resolve,reject) => {
                getToolAccount().then(({code,data,message}) => {
                    if(code === 1){
                        this.toolAcountInfo = data;
                        if(this.toolAcountInfo.status === 2){
                            this.timeout = setTimeout(() => {
                                this.getToolAccount()
                            },5000)
                        }else{
                            clearTimeout(this.timeout)
                        }
                    }else{

                    }
                    resolve()
                })
            })
        },
        opTool(type){
            if(!this.prjId || !this.currentSubnetId){
                return
            }
            this.loading[type] = true;
            let reqApi = type === 'start' ? startToolTunnel :  stopToolTunnel;
            reqApi(this.prjId,this.currentSubnetId).then(async ({code,data,message}) => {
                if(code === 1){
                    this.$Message.info(`${type === 'start' ? '启动' : '关闭'}成功`)
                    this.getToolTunnelDetail(type);
                }else{
                    this.$Message.error(message);
                }
                this.loading[type] = false;
            }).catch(error => {
                console.log(error);
                this.loading[type] = false;
            })
        },
        filterToolStatus(){
            if(!this.toolDetail || !this.toolDetail.status || this.toolDetail.status === 'completed'){
                return '关闭'
            }else if(this.toolDetail.status === 'running'){
                return '开启'
            }else{
                return this.filterTaskStatus(this.toolDetail.status)
            }
        },
        filterTaskStatus(status){
            return this.taskStatusObj[status].label || '未定义'
        },
        filterStatusColor(status){
            return this.taskStatusObj[status].color || 'default'
        },
        renderTaskStatus(h,status){
            return h('Tag',{
                props:{
                    type:'border',
                    color:this.filterStatusColor(status)
                },
            },this.filterTaskStatus(status))
        }
	}
}